import React from 'react';
import { Tools } from 'weaver-mobile';
import { catchApiError } from '../util/index';

const getLabel = Tools.getLabel;
//移动端微博,支持
export default {
    addWindowFunc: () => {
        window.openAppLink = (obj, linkid) => {

            let linkType = obj.attributes.linktype.nodeValue;
            if (linkType == "doc")
                window.openLink.openDocument(linkid);
            else if (linkType == "task")
              window.openLink("/spa/prj/static4mobile/index.html#/TaskDetails/0/"+linkid);
            else if (linkType == "crm")
                window.openLink("/spa/crm/static4mobile/index.html#/customerCard/"+linkid);
            else if (linkType == "workflow")
                window.openLink("/spa/workflow/static4mobileform/index.html#/req?requestid="+linkid);
            else if (linkType == "project")
                window.openLink("/spa/prj/static4mobile/index.html#/ProjectDetails/0/"+linkid);
            else if (linkType == "workplan")
                window.openLink.openWorkplan(linkid);
            return false;
        }

        window.opendoc1 = (showid, obj) => {
            let id =  searchPNode(obj,obj.parentNode.id);
            let options = {
                method: 'POST',
                url: '/api/blog/attach/AddBlogDocShare',
                params: {
                    jsonStr:JSON.stringify({
                        "blogid":id,
                        "docid":showid
                    })}
            };
              Tools.callApi(options).then((result)=>{
                if(result.status=="1"){
                    window.openLink.openDocument(showid,{blogDiscussid:id});
                }
            });


        }
        window.attentionTool= (obj,attentionStatus,islower,userId) => {
            let options = {
                    method: 'POST',
                    url: '/api/blog/base/attentionOpt',
                    params: {attentionStatus:attentionStatus,islower:islower,userId:userId}
                };
               Tools.callApi(options).then((result)=>{
                 document.getElementById(obj).innerHTML=getLabel(18659,'已申请');
               });
        }


        window.downloads = (files, obj,filename) => {
            let id =  searchPNode(obj,obj.parentNode.id);

            let options = {
                method: 'POST',
                url: '/api/blog/attach/AddBlogDocShare',
                params: {
                    jsonStr:JSON.stringify({
                        "blogid":id,
                        "docid":files
                    })}
            };
             Tools.callApi(options).then((result)=>{
               if(result.status=="1"){
                   window.openLink.getWeaverFile("/weaver/weaver.file.FileDownload?fileid=" + files + "&download=1&blogDiscussid="+id)
               }
           });


        }
    }

};
//递归获取父节点
function searchPNode(obj,value) {
    if(value=="wm-comment"){
        return obj.id;
    }else {
        return searchPNode(obj.parentNode,obj.parentNode.className);
    }

}